﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace MethodWorx.Core.DomainModel
{
    public static class DataAnnotationsValidationRunner
    {
        public static IEnumerable<ValidationError> GetErrors(object instance)
        {
            List<ValidationError> errors = new List<ValidationError>();

            errors.AddRange(from prop in TypeDescriptor.GetProperties(instance).Cast<PropertyDescriptor>()
                            from attribute in prop.Attributes.OfType<ValidationAttribute>()
                            where !attribute.IsValid(prop.GetValue(instance))
                            select new ValidationError()
                            {
                                Key = prop.Name,
                                Message = attribute.FormatErrorMessage(string.Empty)
                            });
            /*
            if (instance is IValidatedObject)
            {
                //  add the range in
             * }*/


            return errors;
        }
    }
}
